package com.java2020.online_mall.dao;

import com.alibaba.druid.util.StringUtils;
import com.java2020.online_mall.model.Order;
import com.java2020.online_mall.model.OrderRequestParam;
import com.java2020.online_mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackgeName: com.java2020.online_mall.dao
 * @Author: MNHK
 * Date: 2020/5/2 21:45
 * @Version:1.8
 */

public class OrderDaoImpl implements OrderDao{
    @Override
    public Integer getTotal(OrderRequestParam param) {
        Map<String, Object> map = getDynamicSqlParams(param);
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        String baseSql = (String) map.get("baseSql");
        List sqlParams = (List) map.get("sqlParams");
        Long number = null;
        try {
            number = runner.query("select count(id) from orders " + baseSql, new ScalarHandler<>(), sqlParams.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return number.intValue();
    }

    /**
     * 构建动态sql语句
     * @param param
     * @return
     */
    private Map<String, Object> getDynamicSqlParams(OrderRequestParam param) {
        String baseSql = " where 1 = 1 ";
        List<Object> sqlParams = new ArrayList<>();
        if(param.getState() != -1){
            baseSql = baseSql + " and stateId = ? ";
            sqlParams.add(param.getState());
        }
        if(!StringUtils.isEmpty(param.getMoneyLimit1())){
            baseSql = baseSql + " and amount <= ?";
            sqlParams.add(Double.parseDouble(param.getMoneyLimit1()));
        }
        if(!StringUtils.isEmpty(param.getMoneyLimit2())){
            baseSql = baseSql + " and amount >= ?";
            sqlParams.add(Double.parseDouble(param.getMoneyLimit2()));
        }
        if(!StringUtils.isEmpty(param.getGoods())){
            baseSql = baseSql + " and goods like ?";
            sqlParams.add("%" + param.getGoods() + "%");
        }
        if(!StringUtils.isEmpty(param.getAddress())){
            baseSql = baseSql + " and address like ?";
            sqlParams.add("%" + param.getAddress() + "%");
        }
        if(!StringUtils.isEmpty(param.getName())){
            baseSql = baseSql + " and name like ?";
            sqlParams.add("%" + param.getName() + "%");
        }
        if(!StringUtils.isEmpty(param.getId())){
            baseSql = baseSql + " and id = ?";
            sqlParams.add(param.getId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("baseSql", baseSql);
        map.put("sqlParams", sqlParams);
        return map;
    }

    @Override
    public List<Order> ordersByPage(OrderRequestParam param) {
        Map<String, Object> map = getDynamicSqlParams(param);
        String baseSql = (String) map.get("baseSql");
        List sqlParams = (List) map.get("sqlParams");

        String sql = "select * from orders " + baseSql + "limit ? offset ?";
        sqlParams.add(param.getPagesize());
        sqlParams.add((param.getCurrentPage() - 1) * param.getPagesize());
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Order> orders = null;
        try {
            orders = runner.query(sql, new BeanListHandler<Order>(Order.class), sqlParams.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }
}
